{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "09f7126f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c6ab2b72",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import gc\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import re\n",
    "import time\n",
    "from scipy import stats\n",
    "import matplotlib.pyplot as plt\n",
    "import category_encoders as ce\n",
    "import networkx as nx\n",
    "import pickle\n",
    "import lightgbm as lgb\n",
    "import catboost as cat\n",
    "import xgboost as xgb\n",
    "import seaborn as sns\n",
    "from datetime import timedelta\n",
    "from gensim.models import Word2Vec\n",
    "from io import StringIO\n",
    "from tqdm import tqdm\n",
    "from lightgbm import LGBMClassifier\n",
    "from lightgbm import log_evaluation, early_stopping\n",
    "from sklearn.metrics import roc_curve\n",
    "from scipy.stats import chi2_contingency, pearsonr\n",
    "from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer\n",
    "from sklearn.feature_extraction import FeatureHasher\n",
    "from sklearn.model_selection import StratifiedKFold, KFold, train_test_split, GridSearchCV\n",
    "from category_encoders import TargetEncoder\n",
    "from sklearn.decomposition import TruncatedSVD\n",
    "from autogluon.tabular import TabularDataset, TabularPredictor, FeatureMetadata\n",
    "from autogluon.features.generators import AsTypeFeatureGenerator, BulkFeatureGenerator, DropUniqueFeatureGenerator, FillNaFeatureGenerator, PipelineFeatureGenerator\n",
    "from autogluon.features.generators import CategoryFeatureGenerator, IdentityFeatureGenerator, AutoMLPipelineFeatureGenerator\n",
    "from autogluon.common.features.types import R_INT, R_FLOAT\n",
    "from autogluon.core.metrics import make_scorer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a08d6044",
   "metadata": {},
   "source": [
    "## 数据导入"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df055add",
   "metadata": {},
   "source": [
    "## 通用导入函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "74bcbf7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data_from_directory(directory):\n",
    "    \"\"\"\n",
    "    遍历目录加载所有CSV文件，将其作为独立的DataFrame变量\n",
    "\n",
    "    参数:\n",
    "    - directory: 输入的数据路径\n",
    "    \n",
    "    返回:\n",
    "    - 含有数据集名称的列表\n",
    "    \"\"\"\n",
    "    dataset_names = []\n",
    "    for filename in os.listdir(directory):\n",
    "        if filename.endswith(\".csv\"):\n",
    "            dataset_name = os.path.splitext(filename)[0] + '_data' # 获取文件名作为变量名\n",
    "            file_path = os.path.join(directory, filename)  # 完整的文件路径\n",
    "            globals()[dataset_name] = pd.read_csv(file_path)  # 将文件加载为DataFrame并赋值给全局变量\n",
    "            dataset_names.append(dataset_name)\n",
    "            print(f\"数据集 {dataset_name} 已加载为 DataFrame\")\n",
    "\n",
    "    return dataset_names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "993c86ce",
   "metadata": {},
   "source": [
    "## 训练集导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "722d1e40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>APSDTRDAT</th>\n",
       "      <th>APSDTRTIME</th>\n",
       "      <th>CUST_NO</th>\n",
       "      <th>APSDTRCOD</th>\n",
       "      <th>APSDTRAMT</th>\n",
       "      <th>APSDABS</th>\n",
       "      <th>APSDTRCHL</th>\n",
       "      <th>APSDFLAG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20131201</td>\n",
       "      <td>161612</td>\n",
       "      <td>64d3773c1ef7432af719ad377647c527</td>\n",
       "      <td>31b49b530284874b8d0318c5baf37201</td>\n",
       "      <td>-3.03</td>\n",
       "      <td>b9edc9a85873ce29c6f938fbe7f2f695</td>\n",
       "      <td>30b6329d64f560ec60434d0fba757ee0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20131204</td>\n",
       "      <td>111641</td>\n",
       "      <td>6e9c9993dee7963da55000bdbfbbdace</td>\n",
       "      <td>b8f853d9f1670dd8b94814dab3db8758</td>\n",
       "      <td>0.47</td>\n",
       "      <td>5adaf0bdf909be8bece42bc3dcb32ede</td>\n",
       "      <td>30b6329d64f560ec60434d0fba757ee0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>20131202</td>\n",
       "      <td>121547</td>\n",
       "      <td>e31c2f8c467c36f2919c3e2dfc456792</td>\n",
       "      <td>31b49b530284874b8d0318c5baf37201</td>\n",
       "      <td>-3.69</td>\n",
       "      <td>d822ff958d2e34fd0f3d9eacfaf6b460</td>\n",
       "      <td>30b6329d64f560ec60434d0fba757ee0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20131204</td>\n",
       "      <td>200337</td>\n",
       "      <td>8997f1671ecc5e8e31cc3995fdeeb2fc</td>\n",
       "      <td>6c6f58186b3a7737977acbe5f8068b58</td>\n",
       "      <td>3.69</td>\n",
       "      <td>00537e025c6716eca8f53090d67be7d1</td>\n",
       "      <td>528963653618fdbceaf072cca4231917</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20131202</td>\n",
       "      <td>190833</td>\n",
       "      <td>0e10769bcb9a8c61ffc905d7766403d3</td>\n",
       "      <td>31b49b530284874b8d0318c5baf37201</td>\n",
       "      <td>-2.72</td>\n",
       "      <td>225f339f5c6e96f9166d8721e85538b9</td>\n",
       "      <td>30b6329d64f560ec60434d0fba757ee0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   APSDTRDAT  APSDTRTIME                           CUST_NO  \\\n",
       "0   20131201      161612  64d3773c1ef7432af719ad377647c527   \n",
       "1   20131204      111641  6e9c9993dee7963da55000bdbfbbdace   \n",
       "2   20131202      121547  e31c2f8c467c36f2919c3e2dfc456792   \n",
       "3   20131204      200337  8997f1671ecc5e8e31cc3995fdeeb2fc   \n",
       "4   20131202      190833  0e10769bcb9a8c61ffc905d7766403d3   \n",
       "\n",
       "                          APSDTRCOD  APSDTRAMT  \\\n",
       "0  31b49b530284874b8d0318c5baf37201      -3.03   \n",
       "1  b8f853d9f1670dd8b94814dab3db8758       0.47   \n",
       "2  31b49b530284874b8d0318c5baf37201      -3.69   \n",
       "3  6c6f58186b3a7737977acbe5f8068b58       3.69   \n",
       "4  31b49b530284874b8d0318c5baf37201      -2.72   \n",
       "\n",
       "                            APSDABS                         APSDTRCHL  \\\n",
       "0  b9edc9a85873ce29c6f938fbe7f2f695  30b6329d64f560ec60434d0fba757ee0   \n",
       "1  5adaf0bdf909be8bece42bc3dcb32ede  30b6329d64f560ec60434d0fba757ee0   \n",
       "2  d822ff958d2e34fd0f3d9eacfaf6b460  30b6329d64f560ec60434d0fba757ee0   \n",
       "3  00537e025c6716eca8f53090d67be7d1  528963653618fdbceaf072cca4231917   \n",
       "4  225f339f5c6e96f9166d8721e85538b9  30b6329d64f560ec60434d0fba757ee0   \n",
       "\n",
       "   APSDFLAG  \n",
       "0         1  \n",
       "1         0  \n",
       "2         1  \n",
       "3         0  \n",
       "4         1  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_tr_aps_dtl_data = pd.read_csv('./data/Train/TRAIN_TR_APS_DTL.csv')\n",
    "train_tr_aps_dtl_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82e70968",
   "metadata": {},
   "source": [
    "## 测试集导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3ef0f15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据集 A_ASSET_data 已加载为 DataFrame\n",
      "数据集 A_CCD_TR_DTL_data 已加载为 DataFrame\n",
      "数据集 A_MB_CUST_INFO_data 已加载为 DataFrame\n",
      "数据集 A_MB_PAGEVIEW_DTL_data 已加载为 DataFrame\n",
      "数据集 A_MB_TRNFLW_DTL_data 已加载为 DataFrame\n",
      "数据集 A_PROD_HOLD_data 已加载为 DataFrame\n",
      "数据集 A_TEST_NATURE_data 已加载为 DataFrame\n",
      "数据集 A_TR_APS_DTL_data 已加载为 DataFrame\n"
     ]
    }
   ],
   "source": [
    "#A_tr_aps_dtl_data = pd.read_csv('./data/A/A_TR_APS_DTL.csv')\n",
    "#A_tr_aps_dtl_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6a9129f",
   "metadata": {},
   "source": [
    "# 特征工程"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "starcup",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
